<template>
	<div>
		<van-popup :show="isShowPopup" position="bottom" custom-style="height: 45%;"
			@close="isShowPopup = false;$emit('close' , false)">
			<div style="padding: 10px; box-sizing: border-box;width: 100%; height: 100%;position: relative;">
				<div class="item">
					<div class="image">
						<img :src="data.showImage" alt="" />
					</div>

					<div class="content">
						<p>{{data.commodityName}}</p>
						<p>{{data.commodityDescribe}}</p>
						<p>
							<span>￥{{data.discountPrice || data.commodityPrice}}/<span
									style="font-size: 10px;">{{data.unit || '斤'}}</span></span>
							<span v-if="data.discountPrice">￥{{data.commodityPrice}}/{{data.unit || '斤'}}</span>
						</p>
					</div>
				</div>

				<div class="stepper">
					<span>数量</span>
					<van-stepper :value="number" @change="stepperChange" />
				</div>

				<div class="stepper">
					<span>金额</span>
					<span
						style="color: #FF4137; font-weight: 700;">￥{{(((data.discountPrice || data.commodityPrice) * number).toFixed(2) || 0)}}</span>
				</div>

				<div class="btn">
					<button @click="addCat">加入购物车</button>
					<button>立即购买</button>
				</div>
			</div>
			
			

		</van-popup>
	</div>
</template>

<script>
	import {
		addShoppingCart
	} from '../../api/classfiy';
	export default {
		data() {
			return {
				number: 1,
				isShowPopup: false
			}
		},

		props: ['data', "isShow"],

		watch: {
			isShow: {
				handler(newVal, oldVal) {
					this.isShowPopup = newVal
					this.number = 1
				},
				immediate: true
			}
		},

		methods: {
			stepperChange(e) {
				console.log(e);
				this.number = e.detail
			},

			// 加入购物车 
			async addCat() {
				// 判断 当前用户 是否登录
				let userInfo = uni.getStorageSync("userInfo") || {}
				if (!userInfo.id) {
					uni.navigateTo({
						url: "/subPackages/pages/login/index"
					})
					return
				}

				let parmse = {
					// ...this.data,
					commodityNum: this.number, // 商品数量
					userId: userInfo.id, // 用户 id
					phoneNumber: userInfo.phoneNumber, // 用户手机号
					commodityId: this.data.id // 商品 id
				}
				console.log(parmse);

				let result = await addShoppingCart(parmse)
				console.log(result);
				if (result.code == 20000) {
					uni.showToast({
						title: '商品已加入购物车',
						icon: "none",
						duration: 2000
					})

				} else {
					uni.showToast({
						title: '系统异常，请联系管理员',
						icon: "none",
						duration: 2000
					})
				}
				this.isShowPopup = false
				this.$emit("close", false)
			},
		}
	}
</script>

<style scoped>
	.item {
		width: 100%;
		height: 100px;
		display: flex;
		margin-bottom: 10px;
		border-radius: 5px;
	}

	.item .image {
		width: 80px;
		height: 100%;
		overflow: hidden;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 5px;
	}

	.item .image img {
		height: 100%;
	}

	.item .content {
		margin-left: 5px;
		color: #333;
		display: flex;
		justify-content: space-between;
		flex-direction: column;
		width: calc(100% - 80px);
		padding: 5px;
		box-sizing: border-box;
	}

	.item .content p {
		overflow: hidden;
		width: 100%;
	}

	.item .content p:nth-child(1) {
		font-size: 14px;
		font-weight: 600;
	}

	.item .content p:nth-child(2) {
		font-size: 12px;
		color: #999;
		margin-top: 5px;
	}

	.item .content p:nth-child(3) {
		font-size: 14px;
		color: #FF4F64;
		font-weight: 600;
	}

	.item .content p:nth-child(3) span:nth-child(2) {
		font-size: 10px;
		text-decoration: line-through;
		color: #999;
		font-weight: 400;
	}

	.item .content .commodityBtn {
		width: 100%;
		text-align: right;
		/* background: red; */
	}

	.item .content button {
		width: 30px;
		height: 30px;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0;
		margin-left: auto;
		background-color: #1BA035;
		color: #fff;
	}

	.stepper {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10px;
		box-sizing: border-box;
	}

	.stepper p {
		font-size: 14px;
		color: #333;
	}

	.btn {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: absolute;
		bottom: 0px;
		left: 0;
	}

	.btn button {
		width: 49%;
		height: 40px;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 14px;
		color: #fff;
		font-weight: 700;
		background-color: #1BA035;
		border-radius: 20px;
	}

	.btn button:nth-child(1) {
		border: 1px solid #1BA035;
		color: #333;
		background-color: #fff;
		font-weight: 400;
	}
</style>